<div class="layui-form-item">
    <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</div>
</form>

<script src="../../../../static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    layui.use(['form', 'upload'], function () {
        form = layui.form
        var $ = layui.jquery
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , url: '../../../util/upload/image' //上传接口
            , done: function (res) {
                //上传完毕回调
                if (res.code === 200) {
                    $('#preImg').attr('src', '../../../../' + res.data)
                    $('#pic').attr('value', res.data)
                    layer.msg(res.msg, {icon: 1})
                }
            }
            , error: function () {
                //请求异常回调
                layer.msg('上传失败', {icon: 2});
            }
        });

        form.on('submit(*)', function (data) {
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}

            $.post('update', data.field, function (res) {
                console.log(res)
                if (res.code === 200) {
                    layer.msg(res.msg, {icon: 1})
                    setTimeout(function () {
                        // layer.close(layer.index); //它获取的始终是最新弹出的某个层，值是由layer内部动态递增计算的
                        parent.location.reload()
                    }, 1000)
                } else {
                    layer.msg(res.msg || '请求失败', {icon: 2});
                }
            }, 'json')
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>

</body>
</html>